<?php
//print_r($_REQUEST);
//设置js的目录
define('DS',DIRECTORY_SEPARATOR);
define('WEBROOT',realpath(dirname(__FILE__).'/..')); 
define('JSROOT',WEBROOT.DS.'js'); 
$jslib = array( 
	'global' => JSROOT.DS.'global',
	'module' => WEBROOT.DS.'modules',
);

//取得真实路径
function getPath($lib,$format = false){
	global $jslib;
	if(strlen($lib)<0) return false;
	$lib = trim($lib);
	$odir = preg_split("/[.-_]/",$lib);
	$module = array_shift($odir);
	$fileName = array_pop($odir);
	$root = $jslib[$module];
	//要读取的文件类型 即文件的后缀名
	if($format && is_array($format)){
		$fformat = implode(',',$format);
	} else {
		$fformat = $format;
	}

	//取得子目录
	if('module' == $module){
			array_push($odir,'js');
			array_push($odir,'config');
	}
	$subpath = implode(DS,$odir);

	//配置glob要用的查询条件
	if("*" === $fileName){
		$pattern = $subpath.DS."*.{".$fformat."}";
	} else {//is file
		$pattern = $subpath.DS.$fileName.".{".$fformat."}";
	}

	if(false === $format)
		return $root.DS.$subpath;
	else
		return $root.DS.$pattern;
}

function getFiles($lib,$format='js'){
	$pattern = getPath($lib,$format);
	return glob($pattern,GLOB_BRACE);
}
if($_POST['file']){
	$files = $_POST['file'];
	$ffiles = array();
	if(is_array($files)){
		foreach($files as $module => $tfile){
			if(is_string($module)){
				if(is_array($tfile)){
					foreach($tfile as $file){
						$ffiles = array_merge($ffiles,getFiles($module.'.'.$file));
					}
				} else {
					$ffiles = array_merge($ffiles,getFiles($module.'.'.$tfile));
				}
			} else if(is_int($module)) {
				$ffiles = array_merge($ffiles,getFiles($tfile));	
			}
		}
		foreach($files as  $file){
			if(file_exists($file))
			{	
				$pinfo =pathinfo($file);
				$format = strtolower($pinfo['extension']);
				if('php' === $format) include_once($file);	
				if('js' === $format) echo file_get_contents($file);
				unset($pinfo,$format);
			}
		}

	} else {
		//非数组
		$ffiles = getFiles($files);
	}
}
//
foreach($ffiles as  $file){
	if(file_exists($file))
	{	
		$pinfo =pathinfo($file);
		$format = strtolower($pinfo['extension']);
		if('php' === $format) include_once($file);	
		if('js' === $format) echo file_get_contents($file);
		unset($pinfo,$format);
	}
}
?>
